package com.qf.fcy.youshare.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Alex[FR]
 * Date: 2015/2/4
 * Email: alex_fcy@163.com
 */
public class CommonPagerAdapter extends PagerAdapter {
    private Context context;
    private List<Integer> list;

    public CommonPagerAdapter(Context context, List<Integer> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (list != null) {
            ret = list.size();
        }
        return ret;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView ret = new ImageView(context);
        ret.setScaleType(ImageView.ScaleType.FIT_XY);
        ret.setImageResource(list.get(position));
        container.addView(ret);

        return ret;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof ImageView) {
            ImageView img = (ImageView) object;
            container.removeView(img);
        }
    }
}
